﻿using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Routing;

namespace ALinq.BBS
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                RouteNames.Home, // Route name
                "{action}",
                new { controller = "Home", action = "Index" } // Parameter defaults
            );
            routes.MapRoute(
                RouteNames.Default, // Route name
                "{controller}/{action}",
                new { controller = "Admin", action = "Default" } // Parameter defaults
            );
            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapRoute(
            //    RouteNames.Home, // Route name
            //    "{action}",
            //    new { controller = "Home", action = "Index" } // Parameter defaults
            //);
            routes.MapRoute(
                RouteNames.PostList,
                "PostList/{boardID}/{forumID}/{postListIndex}",
                new { controller = "Home", action = "PostList", postListIndex = UrlParameter.Optional }
            );
            routes.MapRoute(
                RouteNames.Post,
                "Post/{boardID}/{forumID}/{postListIndex}/{postID}/{replyListIndex}",
                new { controller = "Home", action = "Post", replyListIndex = UrlParameter.Optional }
            );
            routes.MapRoute(
                RouteNames.EditPost,
                "EditTopic/{boardID}/{forumID}/{postListIndex}/{postID}",
                new { controller = "Home", action = "EditTopic", postID = UrlParameter.Optional }
            );
            routes.MapRoute(
                RouteNames.EditReply,
                "EditTopic/{boardID}/{forumID}/{postListIndex}/{postID}/{replyListIndex}/{replyID}",
                new { controller = "Home", action = "EditTopic", replyID = UrlParameter.Optional }
            );
            routes.MapRoute(
                RouteNames.DeletePost,
                "DeleteTopic/{boardID}/{forumID}/{postListIndex}/{postID}",
                new { controller = "Home", action = "DeleteTopic", postID = UrlParameter.Optional }
            );
            routes.MapRoute(
                RouteNames.DeleteReply,
                "DeleteTopic/{boardID}/{forumID}/{postListIndex}/{postID}/{replyListIndex}/{replyID}",
                new { controller = "Home", action = "DeleteTopic", replyID = UrlParameter.Optional }
            );
            //routes.MapRoute(
            //    RouteNames.Message,
            //    "Message/{code}/{returnUrl}",
            //    new { controller = "Home", action = "Message", returnUrl = UrlParameter.Optional }
            //    );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            var conn1 = WebConfigurationManager.ConnectionStrings["ALinqBBS"].ConnectionString;
            //var conn1 = WebConfigurationManager.ConnectionStrings["ALinqBBS2"].ConnectionString;
            var conn2 = WebConfigurationManager.ConnectionStrings["ALinqMembership"].ConnectionString;
            ALinq.BBS.DataAccess.DataUtility.SetDatabasePath(conn1);
            ALinq.Web.Security.DataUtility.SetDatabasePath(conn2);
        }

    }
}